package com.clstu.generic;

import org.testng.annotations.Test;

import java.util.*;

public class HomeWork01 {
    public static void main(String[] args) {

    }
    @Test
    public void test(){
        DAO<User> dao = new DAO<>();
        dao.save("01",new User("01",21,"jack"));
        dao.save("02",new User("02",20,"mary"));
        dao.save("03",new User("03",25,"smith"));
        dao.save("04",new User("04",19,"lucy"));
        System.out.println(dao.list());
        System.out.println(dao.get("03"));
        dao.update("03",new User("03",1000,"god"));
        System.out.println(dao.list());
        dao.delete("03");
        System.out.println(" ======删除后===========");
        System.out.println(dao.list());
    }

}
class DAO<T>{
    private Map<String,T> map=new HashMap<>();
    public void save(String id,T t){
        map.put(id,t);
    }
    public T get(String id){
        return map.get(id);
    }
    public void update(String id , T t){
        map.put(id,t);
    }
    public List<T> list(){
        List<T> list=new ArrayList<>();
        Set<String> set = map.keySet();
        for (String s :set
             ) {
            list.add(map.get(s));
        }
        return list;
    }
    public void delete(String id){
        map.remove(id);
    }

}

class User{
    private String id;
    private int age;
    private String name;

    public User(String id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}